using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using DTO;
using System.Data;

namespace HeThongDAO
{
    public class TaiKhoanDAO
    {
        public static Boolean KiemTraDangNhap(TaiKhoanDTO user)
        {
            string sql = "Select * ";
            sql += "From TAI_KHOAN ";
            sql += "Where Username = '" + user.Username + "' And Password = '" + user.Password + "'";

            DataTable dt = Config.AccessHelper.ExecuteQuery(sql);

            if (dt.Rows.Count != 0)
            {
                user.MaLoaiTaiKhoan = Convert.ToInt32(dt.Rows[0]["MaLoaiTaiKhoan"]);
                return true;
            }
            else
            {
                return false;
            }
        }

        public static DataTable LayDanhSachTaiKhoan()
        {
            string sql = "SELECT tk.MaTaiKhoan, tk.Username, tk.Password, l.MaLoaiTaiKhoan, l.TenTaiKhoan ";
            sql += "FROM TAI_KHOAN tk , PHAN_LOAI_TAI_KHOAN l ";
            sql += "WHERE tk.MaLoaiTaiKhoan = l.MaLoaiTaiKhoan";
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        public static bool ThemTaiKhoan(TaiKhoanDTO tkDTO)
        {
            try
            {
                string sql = "INSERT INTO TAI_KHOAN(Username, [Password], MaLoaiTaiKhoan) ";
                sql += "values('" + tkDTO.Username + "','" + tkDTO.Password + "'," + tkDTO.MaLoaiTaiKhoan.ToString() + ")";

                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatTaiKhoan(TaiKhoanDTO tkDTO)
        {
            try
            {
                string sql = "UPDATE TAI_KHOAN SET ";
                sql += "Username = '" + tkDTO.Username + "', ";
                sql += "[Password] = '" + tkDTO.Password + "', ";
                sql += "MaLoaiTaiKhoan = " + tkDTO.MaLoaiTaiKhoan;
                sql += " WHERE MaTaiKhoan = " + tkDTO.MaTaiKhoan;

                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool XoaTaiKhoan(TaiKhoanDTO tkDTO)
        {
            try
            {
                string sql = "DELETE FROM TAI_KHOAN ";
                sql += "WHERE MaTaiKhoan = " + tkDTO.MaTaiKhoan;

                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
